<template>
    <el-card>
        <el-form :model="form" :rules="rules" ref="form" label-position="top" class="flex flex-wrap -m-2">
            <div class="w-1/2 px-2">
                <el-form-item label="登录账号" prop="email">
                    <el-input v-model="form.account">
                    </el-input>
                </el-form-item>
            </div>
            <div class="w-1/2 px-2">
                <el-form-item label="新密码" prop="password">
                    <el-input type="password" v-model.trim="form.password" placeholder="不修改密码，请忽略">
                    </el-input>
                </el-form-item>
                <el-form-item label="确认新密码" prop="confirm_password">
                    <el-input type="password" v-model.trim="form.password_confirmation" placeholder="不修改密码，请忽略">
                    </el-input>
                </el-form-item>
            </div>
            <div class="w-full flex justify-center mt1">
                <el-button type="primary" @click="submit" :loading="isBtnLoading">保存</el-button>
            </div>
        </el-form>
    </el-card>
</template>

<script>
import {Form, FormItem, Input} from 'element-ui';

export default {
    components: {
        ElForm: Form,
        ElFormItem: FormItem,
        ElInput: Input,
    },
    props: [],
    data() {
        return {
            form: {
                account: this.$store.state.my.account,
                password: '',
                password_confirmation: '',
            },
            rules: {
                account: [
                    {required: true, message: '请输入登录账号'},
                ],
            },
            isBtnLoading: false,
        };
    },
    computed: {},
    watch: {},
    methods: {
        submit() {
            this.$refs.form.validate((valid) => {
                if (!valid) return;
                if (this.form.password !== this.form.password_confirmation) {
                    $ele.$message.error('两次密码不一样');
                    return;
                }
                this.isBtnLoading = true;
                API.put('my/profile', this.form)
                    .then(() => {
                        this.$store.dispatch('getMy');
                        $ele.$message.success('修改成功');
                        this.$router.push('/');
                    })
                    .finally(() => (this.isBtnLoading = false));
            });
        },
    },
    mounted() {
    },
};
</script>

<style lang="scss">
</style>
